package com.sedna.recipenote.common.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sedna.recipenote.framework.value.ErrorValue;

public class BaseController {
	private Logger logger = Logger.getLogger(BaseController.class);

	@ExceptionHandler
	@ResponseBody
	 public ErrorValue handleRuntimeException(RuntimeException r, HttpServletResponse response) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		ErrorValue errorValue = new ErrorValue(r.getMessage(), r.toString());
		response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
		//modelMap.put("stacktrace", r.getStackTrace());
		logger.error("RuntimeExcpetion: " + r.toString());
        return errorValue;
    }

	@ExceptionHandler
	@ResponseBody
	 public ErrorValue handleRuntimeException(IOException ioException, HttpServletResponse response) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		ErrorValue errorValue = new ErrorValue(ioException.getMessage(), ioException.toString());
		response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
		//modelMap.put("stacktrace", r.getStackTrace());
		logger.error("RuntimeExcpetion: " + ioException.toString());
        return errorValue;
    }

}
